import java.util.Stack;

class Solution {
    public boolean validateStackSequences(int[] pushed, int[] popped) {
        Stack<Integer> pu = new Stack<>();
        int n = pushed.length;
        int k = 0;
        for (int i = 0; i < n; i++) {
            pu.push(pushed[i]);
            while (!pu.isEmpty() && pu.peek() == popped[k]) {
                pu.pop();
                k++;
            }
        }
        if (k >= n) return true;
        return false;
    }
}